//
//  mainCollectionView.m
//  store
//
//  Created by zhouzhouzhou on 2018/9/26.
//  Copyright © 2018年 zhou. All rights reserved.
//

#import "STMainCollectionView.h"
#import "ZYHColLabBtnCell.h"

@implementation STMainCollectionView


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    collectionFrame=frame;
    self.backgroundColor=[UIColor clearColor];
    return self;
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    collectionFrame=frame;
    mainCollectionView.frame = self.bounds;
}

-(void)setMainSpaceCellView{
    float viewW =collectionFrame.size.width;
    float viewH =collectionFrame.size.height;
    NSLog(@"w = %f, h = %f",viewW,viewH);
    //    float Wall=kGTSDeviceFullWidth - 40;
    float w = (viewW-11) / 3;
    float h = w;
    
    UICollectionViewFlowLayout * flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize=CGSizeMake(w, h);
    flow.headerReferenceSize = CGSizeMake(SCREEN_WIDTH,15.0f);
    flow.sectionInset=UIEdgeInsetsMake(0,0,0,0);
    mainCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, viewW, viewH-49) collectionViewLayout:flow];
    [mainCollectionView registerClass:[ZYHColLabBtnCell class] forCellWithReuseIdentifier:@"mainLabBtnCell"];
    [mainCollectionView registerClass:[UICollectionReusableView class]
           forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                  withReuseIdentifier:@"headerView"];
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
//    mainCollectionView.scrollEnabled=NO;
    mainCollectionView.showsHorizontalScrollIndicator = NO;
    mainCollectionView.showsVerticalScrollIndicator = NO;
    mainCollectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:mainCollectionView];
}

#pragma mark -collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.mainCellArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray * arr = self.mainCellArr[section];
    return arr.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *selfreusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *selfheaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                             withReuseIdentifier:@"headerView"
                                                                                                    forIndexPath:indexPath];
        selfreusableview = selfheaderView;
    }
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    headView.backgroundColor = [UIColor clearColor];
    [selfreusableview addSubview:headView];
    selfreusableview.backgroundColor = [UIColor clearColor];
  
    return selfreusableview;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

//
//// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"mainLabBtnCell";
    ZYHColLabBtnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    NSArray * arrSection =self.mainCellArr[indexPath.section];
    NSDictionary * sceneDetail=arrSection[indexPath.row];
    cell.nameLab.text=sceneDetail[@"name"];
    
    if ([sceneDetail.allKeys containsObject:@"img"]) {
        cell.imageSpace.image=[UIImage imageNamed:sceneDetail[@"img"]];
    }else{
        cell.imageSpace.image=[UIImage imageNamed:@"mps_otherImage"];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(mainSpaceView:didSelectItemAtIndexPath:)]) {
        [self.delegate mainSpaceView:self didSelectItemAtIndexPath:indexPath];
    }
}


@end
